﻿using System;
using System.Windows.Browser;
using Infrastructure.Controls;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.ServiceLocation;
using MVVMBasic.View.Login;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace MVVMBasic
{
    /// <summary>
    /// <see cref="UserControl"/> class providing the main UI for the application.
    /// </summary>
    public partial class MainPage
    {
        /// <summary>
        /// Creates a new <see cref="MainPage"/> instance.
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            this.loginContainer.Child = new LoginStatus();
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (HtmlPage.Document.QueryString.ContainsKey("actcode"))
            {
                var activationWindow = new ActivationWindow();
                activationWindow.Show();
            }
        }

        /// <summary>
        /// After the Frame navigates, ensure the <see cref="HyperlinkButton"/> representing the current page is selected
        /// </summary>
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                var hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    VisualStateManager.GoToState(hb,
                                                 hb.NavigateUri.ToString().Equals(e.Uri.ToString())
                                                     ? "ActiveLink"
                                                     : "InactiveLink", true);
                }
            }
        }

        /// <summary>
        /// If an error occurs during navigation, show an error window
        /// </summary>
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            var errWnd = new ErrorWindow { Message = e.Exception.Message };
            errWnd.Show();
        }

        private void ContentFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            var url = e.Uri.ToString();
            if (url.Contains("Employees"))
            {
                var moduleManager = ServiceLocator.Current.GetInstance<IModuleManager>();
                moduleManager.LoadModule("HumanResourcesModule");
                moduleManager.LoadModuleCompleted += moduleManager_LoadModuleCompleted;
                e.Cancel = true;
            }


        }

        void moduleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                ContentFrame.Navigating -= ContentFrame_Navigating;
                ContentFrame.Navigate(new Uri("/MainRegionPage", UriKind.Relative));
                ContentFrame.Navigating += ContentFrame_Navigating;
            }
        }
    }
}